home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / lib / python2.4 / test / test_htmlparser.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2005-10-18  |  12.5 KB  |  375 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.4)
  3.  
  4. '''Tests for HTMLParser.py.'''
  5. import HTMLParser
  6. import pprint
  7. import sys
  8. import unittest
  9. from test import test_support
  10.  
  11. class EventCollector(HTMLParser.HTMLParser):
  12.     
  13.     def __init__(self):
  14.         self.events = []
  15.         self.append = self.events.append
  16.         HTMLParser.HTMLParser.__init__(self)
  17.  
  18.     
  19.     def get_events(self):
  20.         L = []
  21.         prevtype = None
  22.         for event in self.events:
  23.             type = event[0]
  24.             if prevtype == prevtype:
  25.                 pass
  26.             elif prevtype == 'data':
  27.                 L[-1] = ('data', L[-1][1] + event[1])
  28.             else:
  29.                 L.append(event)
  30.             prevtype = type
  31.         
  32.         self.events = L
  33.         return L
  34.  
  35.     
  36.     def handle_starttag(self, tag, attrs):
  37.         self.append(('starttag', tag, attrs))
  38.  
  39.     
  40.     def handle_startendtag(self, tag, attrs):
  41.         self.append(('startendtag', tag, attrs))
  42.  
  43.     
  44.     def handle_endtag(self, tag):
  45.         self.append(('endtag', tag))
  46.  
  47.     
  48.     def handle_comment(self, data):
  49.         self.append(('comment', data))
  50.  
  51.     
  52.     def handle_charref(self, data):
  53.         self.append(('charref', data))
  54.  
  55.     
  56.     def handle_data(self, data):
  57.         self.append(('data', data))
  58.  
  59.     
  60.     def handle_decl(self, data):
  61.         self.append(('decl', data))
  62.  
  63.     
  64.     def handle_entityref(self, data):
  65.         self.append(('entityref', data))
  66.  
  67.     
  68.     def handle_pi(self, data):
  69.         self.append(('pi', data))
  70.  
  71.     
  72.     def unknown_decl(self, decl):
  73.         self.append(('unknown decl', decl))
  74.  
  75.  
  76.  
  77. class EventCollectorExtra(EventCollector):
  78.     
  79.     def handle_starttag(self, tag, attrs):
  80.         EventCollector.handle_starttag(self, tag, attrs)
  81.         self.append(('starttag_text', self.get_starttag_text()))
  82.  
  83.  
  84.  
  85. class TestCaseBase(unittest.TestCase):
  86.     
  87.     def _run_check(self, source, expected_events, collector = EventCollector):
  88.         parser = collector()
  89.         for s in source:
  90.             parser.feed(s)
  91.         
  92.         parser.close()
  93.         events = parser.get_events()
  94.         if events != expected_events:
  95.             self.fail('received events did not match expected events\nExpected:\n' + pprint.pformat(expected_events) + '\nReceived:\n' + pprint.pformat(events))
  96.         
  97.  
  98.     
  99.     def _run_check_extra(self, source, events):
  100.         self._run_check(source, events, EventCollectorExtra)
  101.  
  102.     
  103.     def _parse_error(self, source):
  104.         
  105.         def parse(source = source):
  106.             parser = HTMLParser.HTMLParser()
  107.             parser.feed(source)
  108.             parser.close()
  109.  
  110.         self.assertRaises(HTMLParser.HTMLParseError, parse)
  111.  
  112.  
  113.  
  114. class HTMLParserTestCase(TestCaseBase):
  115.     
  116.     def test_processing_instruction_only(self):
  117.         self._run_check('<?processing instruction>', [
  118.             ('pi', 'processing instruction')])
  119.         self._run_check('<?processing instruction ?>', [
  120.             ('pi', 'processing instruction ?')])
  121.  
  122.     
  123.     def test_simple_html(self):
  124.         self._run_check("\n<!DOCTYPE html PUBLIC 'foo'>\n<HTML>&entity; \n<!--comment1a\n-></foo><bar><<?pi?></foo<bar\ncomment1b-->\n<Img sRc='Bar' isMAP>sample\ntext\n“\n<!--comment2a-- --comment2b-->\n</Html>\n", [
  125.             ('data', '\n'),
  126.             ('decl', "DOCTYPE html PUBLIC 'foo'"),
  127.             ('data', '\n'),
  128.             ('starttag', 'html', []),
  129.             ('entityref', 'entity'),
  130.             ('charref', '32'),
  131.             ('data', '\n'),
  132.             ('comment', 'comment1a\n-></foo><bar><<?pi?></foo<bar\ncomment1b'),
  133.             ('data', '\n'),
  134.             ('starttag', 'img', [
  135.                 ('src', 'Bar'),
  136.                 ('ismap', None)]),
  137.             ('data', 'sample\ntext\n'),
  138.             ('charref', 'x201C'),
  139.             ('data', '\n'),
  140.             ('comment', 'comment2a-- --comment2b'),
  141.             ('data', '\n'),
  142.             ('endtag', 'html'),
  143.             ('data', '\n')])
  144.  
  145.     
  146.     def test_unclosed_entityref(self):
  147.         self._run_check('&entityref foo', [
  148.             ('entityref', 'entityref'),
  149.             ('data', ' foo')])
  150.  
  151.     
  152.     def test_doctype_decl(self):
  153.         inside = "DOCTYPE html [\n  <!ELEMENT html - O EMPTY>\n  <!ATTLIST html\n      version CDATA #IMPLIED\n      profile CDATA 'DublinCore'>\n  <!NOTATION datatype SYSTEM 'http://xml.python.org/notations/python-module'>\n  <!ENTITY myEntity 'internal parsed entity'>\n  <!ENTITY anEntity SYSTEM 'http://xml.python.org/entities/something.xml'>\n  <!ENTITY % paramEntity 'name|name|name'>\n  %paramEntity;\n  <!-- comment -->\n]"
  154.         self._run_check('<!%s>' % inside, [
  155.             ('decl', inside)])
  156.  
  157.     
  158.     def test_bad_nesting(self):
  159.         self._run_check('<a><b></a></b>', [
  160.             ('starttag', 'a', []),
  161.             ('starttag', 'b', []),
  162.             ('endtag', 'a'),
  163.             ('endtag', 'b')])
  164.  
  165.     
  166.     def test_bare_ampersands(self):
  167.         self._run_check('this text & contains & ampersands &', [
  168.             ('data', 'this text & contains & ampersands &')])
  169.  
  170.     
  171.     def test_bare_pointy_brackets(self):
  172.         self._run_check('this < text > contains < bare>pointy< brackets', [
  173.             ('data', 'this < text > contains < bare>pointy< brackets')])
  174.  
  175.     
  176.     def test_attr_syntax(self):
  177.         output = [
  178.             ('starttag', 'a', [
  179.                 ('b', 'v'),
  180.                 ('c', 'v'),
  181.                 ('d', 'v'),
  182.                 ('e', None)])]
  183.         self._run_check('<a b=\'v\' c="v" d=v e>', output)
  184.         self._run_check('<a  b = \'v\' c = "v" d = v e>', output)
  185.         self._run_check('<a\nb\n=\n\'v\'\nc\n=\n"v"\nd\n=\nv\ne>', output)
  186.         self._run_check('<a\tb\t=\t\'v\'\tc\t=\t"v"\td\t=\tv\te>', output)
  187.  
  188.     
  189.     def test_attr_values(self):
  190.         self._run_check('<a b=\'xxx\n\txxx\' c="yyy\t\nyyy" d=\'\txyz\n\'>', [
  191.             ('starttag', 'a', [
  192.                 ('b', 'xxx\n\txxx'),
  193.                 ('c', 'yyy\t\nyyy'),
  194.                 ('d', '\txyz\n')])])
  195.         self._run_check('<a b=\'\' c="">', [
  196.             ('starttag', 'a', [
  197.                 ('b', ''),
  198.                 ('c', '')])])
  199.         self._run_check('<e a=rgb(1,2,3)>', [
  200.             ('starttag', 'e', [
  201.                 ('a', 'rgb(1,2,3)')])])
  202.         self._run_check('<a href=mailto:xyz@example.com>', [
  203.             ('starttag', 'a', [
  204.                 ('href', 'mailto:xyz@example.com')])])
  205.  
  206.     
  207.     def test_attr_entity_replacement(self):
  208.         self._run_check("<a b='&><"''>", [
  209.             ('starttag', 'a', [
  210.                 ('b', '&><"\'')])])
  211.  
  212.     
  213.     def test_attr_funky_names(self):
  214.         self._run_check("<a a.b='v' c:d=v e-f=v>", [
  215.             ('starttag', 'a', [
  216.                 ('a.b', 'v'),
  217.                 ('c:d', 'v'),
  218.                 ('e-f', 'v')])])
  219.  
  220.     
  221.     def test_illegal_declarations(self):
  222.         self._parse_error('<!spacer type="block" height="25">')
  223.  
  224.     
  225.     def test_starttag_end_boundary(self):
  226.         self._run_check("<a b='<'>", [
  227.             ('starttag', 'a', [
  228.                 ('b', '<')])])
  229.         self._run_check("<a b='>'>", [
  230.             ('starttag', 'a', [
  231.                 ('b', '>')])])
  232.  
  233.     
  234.     def test_buffer_artefacts(self):
  235.         output = [
  236.             ('starttag', 'a', [
  237.                 ('b', '<')])]
  238.         self._run_check([
  239.             "<a b='<'>"], output)
  240.         self._run_check([
  241.             '<a ',
  242.             "b='<'>"], output)
  243.         self._run_check([
  244.             '<a b',
  245.             "='<'>"], output)
  246.         self._run_check([
  247.             '<a b=',
  248.             "'<'>"], output)
  249.         self._run_check([
  250.             "<a b='<",
  251.             "'>"], output)
  252.         self._run_check([
  253.             "<a b='<'",
  254.             '>'], output)
  255.         output = [
  256.             ('starttag', 'a', [
  257.                 ('b', '>')])]
  258.         self._run_check([
  259.             "<a b='>'>"], output)
  260.         self._run_check([
  261.             '<a ',
  262.             "b='>'>"], output)
  263.         self._run_check([
  264.             '<a b',
  265.             "='>'>"], output)
  266.         self._run_check([
  267.             '<a b=',
  268.             "'>'>"], output)
  269.         self._run_check([
  270.             "<a b='>",
  271.             "'>"], output)
  272.         self._run_check([
  273.             "<a b='>'",
  274.             '>'], output)
  275.         output = [
  276.             ('comment', 'abc')]
  277.         self._run_check([
  278.             '',
  279.             '<!--abc-->'], output)
  280.         self._run_check([
  281.             '<',
  282.             '!--abc-->'], output)
  283.         self._run_check([
  284.             '<!',
  285.             '--abc-->'], output)
  286.         self._run_check([
  287.             '<!-',
  288.             '-abc-->'], output)
  289.         self._run_check([
  290.             '<!--',
  291.             'abc-->'], output)
  292.         self._run_check([
  293.             '<!--a',
  294.             'bc-->'], output)
  295.         self._run_check([
  296.             '<!--ab',
  297.             'c-->'], output)
  298.         self._run_check([
  299.             '<!--abc',
  300.             '-->'], output)
  301.         self._run_check([
  302.             '<!--abc-',
  303.             '->'], output)
  304.         self._run_check([
  305.             '<!--abc--',
  306.             '>'], output)
  307.         self._run_check([
  308.             '<!--abc-->',
  309.             ''], output)
  310.  
  311.     
  312.     def test_starttag_junk_chars(self):
  313.         self._parse_error('</>')
  314.         self._parse_error('</$>')
  315.         self._parse_error('</')
  316.         self._parse_error('</a')
  317.         self._parse_error('<a<a>')
  318.         self._parse_error('</a<a>')
  319.         self._parse_error('<!')
  320.         self._parse_error('<a $>')
  321.         self._parse_error('<a')
  322.         self._parse_error("<a foo='bar'")
  323.         self._parse_error("<a foo='bar")
  324.         self._parse_error("<a foo='>'")
  325.         self._parse_error("<a foo='>")
  326.         self._parse_error('<a foo=>')
  327.  
  328.     
  329.     def test_declaration_junk_chars(self):
  330.         self._parse_error('<!DOCTYPE foo $ >')
  331.  
  332.     
  333.     def test_startendtag(self):
  334.         self._run_check('<p/>', [
  335.             ('startendtag', 'p', [])])
  336.         self._run_check('<p></p>', [
  337.             ('starttag', 'p', []),
  338.             ('endtag', 'p')])
  339.         self._run_check("<p><img src='foo' /></p>", [
  340.             ('starttag', 'p', []),
  341.             ('startendtag', 'img', [
  342.                 ('src', 'foo')]),
  343.             ('endtag', 'p')])
  344.  
  345.     
  346.     def test_get_starttag_text(self):
  347.         s = '<foo:bar   \n   one="1"\ttwo=2   >'
  348.         self._run_check_extra(s, [
  349.             ('starttag', 'foo:bar', [
  350.                 ('one', '1'),
  351.                 ('two', '2')]),
  352.             ('starttag_text', s)])
  353.  
  354.     
  355.     def test_cdata_content(self):
  356.         s = '<script> <!-- not a comment --> ¬-an-entity-ref; </script>'
  357.         self._run_check(s, [
  358.             ('starttag', 'script', []),
  359.             ('data', ' <!-- not a comment --> ¬-an-entity-ref; '),
  360.             ('endtag', 'script')])
  361.         s = "<script> <not a='start tag'> </script>"
  362.         self._run_check(s, [
  363.             ('starttag', 'script', []),
  364.             ('data', " <not a='start tag'> "),
  365.             ('endtag', 'script')])
  366.  
  367.  
  368.  
  369. def test_main():
  370.     test_support.run_unittest(HTMLParserTestCase)
  371.  
  372. if __name__ == '__main__':
  373.     test_main()
  374.  
  375.